package com.freesky.biz.icbc.service.impl;

import com.freesky.biz.icbc.domain.TIcbcHappyIot;
import com.freesky.biz.icbc.mapper.IcbcHappyIotMapper;
import com.freesky.biz.icbc.service.IIcbcHappyIotService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IcbcHappyIotServiceImpl implements IIcbcHappyIotService {

    @Autowired
    private IcbcHappyIotMapper happyIotMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;


    @Override
    public List<TIcbcHappyIot> findList(TIcbcHappyIot happyIot) {
        return happyIotMapper.findList(happyIot);
    }

    @Override
    public AjaxResult exportFile(TIcbcHappyIot happyIot) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("HAPPY_IOT");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(happyIot, new TIcbcHappyIot(), IIcbcHappyIotService.class,
                "findList", "集福气红包",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }
}
